<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="代码" prop="code">
        <el-select v-model="queryParams.code" :show-count="true" filterable placeholder="请选择代码">
          <el-option v-for="item in codeOptions" :key="item.code" :label="item.code" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="交易日期">
        <el-date-picker
          v-model="daterangeDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"><el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['binance:market:export']">导出</el-button></el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="marketList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="代码" align="center" prop="code" width="120" />
      <el-table-column label="交易日期" align="center" prop="date" width="100" />
      <el-table-column label="当日开盘时间" align="center" prop="openTime" width="160"></el-table-column>
      <el-table-column label="当日收盘时间" align="center" prop="closeTime" width="160"></el-table-column>
      <el-table-column label="开盘价" align="center" prop="openPrice" />
      <el-table-column label="当日最高价" align="center" prop="highPrice" />
      <el-table-column label="当日最低价" align="center" prop="lowPrice" />
      <el-table-column label="收盘价" align="center" prop="lastPrice" />
      <el-table-column label="绝对价格变动" align="center" prop="priceChange" />
      <el-table-column label="相对价格变动百分比" align="center" prop="priceChangePercent" />
      <el-table-column label="加权均价" align="center" prop="weightedAvgPrice" />
      <el-table-column label="基础货币成交量" align="center" prop="volume" />
      <el-table-column label="计价货币成交量" align="center" prop="quoteVolume" />
      <el-table-column label="当日交易笔数" align="center" prop="tradeCount" />
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改日行情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px"></el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMarket, getMarket, delMarket, addMarket, updateMarket } from '@/api/binance/market';
import { listAllMonitoring } from '@/api/binance/kConf';

export default {
  name: 'Market',
  data() {
    return {
      codeOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日行情表格数据
      marketList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        date: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.initOptions();
  },
  methods: {
    initOptions() {
      listAllMonitoring().then(response => {
        this.codeOptions = response.data;
      });
    },
    /** 查询日行情列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDate && '' != this.daterangeDate) {
        this.queryParams.params['beginDate'] = this.daterangeDate[0];
        this.queryParams.params['endDate'] = this.daterangeDate[1];
      }
      listMarket(this.queryParams).then(response => {
        this.marketList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        code: null,
        date: null,
        openTime: null,
        closeTime: null,
        openPrice: null,
        highPrice: null,
        lowPrice: null,
        lastPrice: null,
        priceChange: null,
        priceChangePercent: null,
        weightedAvgPrice: null,
        volume: null,
        quoteVolume: null,
        firstTradeId: null,
        lastTradeId: null,
        tradeCount: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDate = [];
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.code);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加日行情';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const code = row.code || this.ids;
      getMarket(code).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改日行情';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.code != null) {
            updateMarket(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addMarket(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const codes = row.code || this.ids;
      this.$modal
        .confirm('是否确认删除日行情编号为"' + codes + '"的数据项？')
        .then(function() {
          return delMarket(codes);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'binance/market/export',
        {
          ...this.queryParams
        },
        `market_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
